﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PRODUCT.Product.DAO;
using ERP_RORZE_DLL.Product.Base;
using System.Data.OleDb;
using ERP_RORZE_SYSTEM;
using System.Data.Common;
using System.Data.SqlClient;

namespace ERP_RORZE_PRODUCT.Product.Incauhinh
{
    public partial class ctrlMaerialForm : DevExpress.XtraEditors.XtraUserControl
    {
        ClsIncauhinhDAO clsConfigureDAO = new ClsIncauhinhDAO();
        DataTable dt;
        ClsMaterialFormTable b = new ClsMaterialFormTable();

        public ctrlMaerialForm()
        {
            InitializeComponent();
        }

        void Reload(object sender, EventArgs e)
        {
            dt = clsConfigureDAO.GetAllMaterialForm();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void ctrlMaerialForm_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frm_MaterialFormAdd frmAdd = new frm_MaterialFormAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnAddNew_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            frm_MaterialFormEdit frmEdit = new frm_MaterialFormEdit();
            frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"ID").ToString();
            frmEdit.Name = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialFormID").ToString();
            frmEdit.Reload += new EventHandler(Reload);
            frmEdit.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            clsConfigureDAO.DeleteMaterialForm(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString());
            Reload(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            btDelete_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                OleDbConnection connection = new OleDbConnection();
                try
                {
                    string path = fileDialog.FileName;
                    INISettings iniSettings = new INISettings();
                    string connectString = iniSettings.SQLConnectionString;
                    // Connection String to Excel Workbook
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False", path);

                    connection.ConnectionString = excelConnectionString;
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
                    // Create DbDataReader to Data Worksheet
                    DbDataReader dr = command.ExecuteReader();

                    // Bulk Copy to SQL Server 
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);
                    bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_MaterialForm]";

                    SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping("MaterialFormID", "MaterialFormID");
                    bulkInsert.ColumnMappings.Add(date);

                    bulkInsert.WriteToServer(dr);

                    connection.Close();
                    Reload(sender, e);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    connection.Close();
                }
            }
        }
    }
}
